﻿using System;
using Company.BLL.GC;
using Company.BLL;
using Company.BLL.DuLieuChuan;
using Janus.Windows.GridEX;
using System.Windows.Forms;
using Company.BLL.KDT;

namespace Company.Interface.GC
{
    public partial class HopDongRegistedDetailForm : BaseForm
    {
        public HopDong HD = new HopDong();
        public NguyenPhuLieu NguyenPhuLieuSelected = new NguyenPhuLieu();
        public bool IsBrowseForm = false;
        public HopDongRegistedDetailForm()
        {
            InitializeComponent();
        }

        private void khoitao_DuLieuChuan()
        {
            this._DonViHaiQuan = DonViHaiQuan.SelectAll();
            this._DonViTinh = DonViTinh.SelectAll();
        }
        
        private void HopDong_Form_Load(object sender, EventArgs e)
        {
            this.khoitao_DuLieuChuan();
            ctrDonViHaiQuan.Ma = this.HD.MaHaiQuan;
            txtSoHopDong.Text = this.HD.SoHopDong;
            ccNgayKyHD.Value = this.HD.NgayKy;
            ccNgayKetThucHD.Value = this.HD.NgayHetHan;
            nuocThue.Ma = HD.NuocThue_ID;
            nguyenTeControl1.Ma = HD.NguyenTe_ID;
            if (this.HD.NgayGiaHan.Year != 1900)
                ccNgayGiaHan.Value = this.HD.NgayGiaHan;
            tabHopDong.SelectedIndex = 0;

            Company.BLL.GC.NhomSanPham nhomsp = new Company.BLL.GC.NhomSanPham();
            nhomsp.SoHopDong = this.HD.SoHopDong;
            nhomsp.MaHaiQuan = this.HD.MaHaiQuan;
            nhomsp.MaDoanhNghiep = this.HD.MaDoanhNghiep;
            nhomsp.NgayKy = this.HD.NgayKy;
            dgNhomSanPham.DataSource = nhomsp.SelectCollectionBy_SoHopDong_AND_MaHaiQuan_AND_MaDoanhNghiep_AND_NgayKy();
            //dgNguyenPhuLieu.Refetch();            
        }

        private void tabHopDong_SelectedTabChanged(object sender, Janus.Windows.UI.Tab.TabEventArgs e)
        {
            switch (e.Page.Name)
            {
                case "tpNguyenPhuLieu":
                    NguyenPhuLieu NPL = new NguyenPhuLieu();
                    NPL.SoHopDong = this.HD.SoHopDong;
                    NPL.MaHaiQuan = this.HD.MaHaiQuan;
                    NPL.MaDoanhNghiep = this.HD.MaDoanhNghiep;
                    NPL.NgayKy = this.HD.NgayKy;
                    dgNguyenPhuLieu.DataSource = NPL.SelectCollectionBy_SoHopDong_AND_MaHaiQuan_AND_MaDoanhNghiep_AND_NgayKy();
                    dgNguyenPhuLieu.Refetch();
                    break;
                case "tpSanPham":
                    SanPham SP = new SanPham();
                    SP.SoHopDong = this.HD.SoHopDong;
                    SP.MaHaiQuan = this.HD.MaHaiQuan;
                    SP.MaDoanhNghiep = this.HD.MaDoanhNghiep;
                    SP.NgayKy = this.HD.NgayKy;
                    dgSanPham.DataSource = SP.SelectCollectionBy_SoHopDong_AND_MaHaiQuan_AND_MaDoanhNghiep_AND_NgayKy();
                    dgSanPham.Refetch();
                    break;
               case "tpThietBi":
                    ThietBi TB = new ThietBi();
                    TB.SoHopDong = this.HD.SoHopDong;
                    TB.MaHaiQuan = this.HD.MaHaiQuan;
                    TB.MaDoanhNghiep = this.HD.MaDoanhNghiep;
                    TB.NgayKy = this.HD.NgayKy;
                    dgThietBi.DataSource = TB.SelectCollectionBy_SoHopDong_AND_MaHaiQuan_AND_MaDoanhNghiep_AND_NgayKy();
                    dgThietBi.Refetch();
                    break;

            }
        }

        private void ctrDonViHaiQuan_ValueChanged(object sender, EventArgs e)
        {

        }

        private void dgNguyenPhuLieu_LoadingRow(object sender, RowLoadEventArgs e)
        {
            if (e.Row.RowType == RowType.Record)
                e.Row.Cells["DVT_ID"].Text = this.DonViTinh_GetName(e.Row.Cells["DVT_ID"].Value.ToString());
        }

        private void dgSanPham_LoadingRow(object sender, RowLoadEventArgs e)
        {
            if (e.Row.RowType == RowType.Record)
            {
                e.Row.Cells["DVT_ID"].Text = this.DonViTinh_GetName(e.Row.Cells["DVT_ID"].Value.ToString());
                if(e.Row.Cells["NhomSanPham"].Text!="")
                    e.Row.Cells["NhomSanPham"].Text = this.NhomSanPham_GetName(e.Row.Cells["NhomSanPham"].Value.ToString());
            }
        }

        private void dgNguyenPhuLieu_RowDoubleClick(object sender, RowActionEventArgs e)
        {
            if (this.IsBrowseForm)
            {
                if (e.Row.RowType == RowType.Record)
                {
                    string maNPL = e.Row.Cells["Ma"].Text;
                    this.NguyenPhuLieuSelected.Ma = maNPL;
                    this.NguyenPhuLieuSelected.MaHaiQuan = this.HD.MaHaiQuan;
                    this.NguyenPhuLieuSelected.MaDoanhNghiep = this.HD.MaDoanhNghiep;
                    this.NguyenPhuLieuSelected.SoHopDong = this.HD.SoHopDong;
                    this.NguyenPhuLieuSelected.NgayKy = this.HD.NgayKy;
                    this.NguyenPhuLieuSelected.Load();
                    this.Close();
                }
            }
        }

        private void dgThietBi_LoadingRow(object sender, RowLoadEventArgs e)
        {
            if (e.Row.RowType == RowType.Record)
            {
                e.Row.Cells["DVT_ID"].Text = this.DonViTinh_GetName(e.Row.Cells["DVT_ID"].Value.ToString());
                e.Row.Cells["XuatXu"].Text = this.Nuoc_GetName(e.Row.Cells["XuatXu"].Value.ToString());
                e.Row.Cells["NguyenTe"].Text = this.NguyenTe_GetName(e.Row.Cells["NguyenTe"].Value.ToString());
            }

        }

        private void cmMainHDGC_CommandClick(object sender, Janus.Windows.UI.CommandBars.CommandEventArgs e)
        {
            switch (e.Command.Key)
            {
                case "ThemPK": showFukien(); break;
                case "Dong": this.Close(); break;
                case "NhanDuLieu": DongBo(); break;
            }
        }
        private void DongBo()
        {            
            WSForm wsForm = new WSForm();
            wsForm.ShowDialog(this);
            if (!wsForm.IsReady) return;
            try
            {
                this.Cursor = Cursors.WaitCursor;
                HD.DongBoHaiQuan();
                this.Cursor = Cursors.Default;
                ShowMessage("Đã đồng bộ thành công", false);
            }
            catch (Exception ex)
            {
                this.Cursor = Cursors.Default;
                if (ShowMessage("Có lỗi khi thực hiện. Bạn có muốn đưa vào hàng đợi không" + ex.Message, true) == "Yes")
                {
                    HangDoi hd = new HangDoi();
                    hd.ID = 0;
                    hd.MaHaiQuan = HD.MaHaiQuan;
                    hd.NgayKy = HD.NgayKy;
                    hd.SoHopDong = HD.SoHopDong;
                    hd.LoaiToKhai = LoaiToKhai.HOP_DONG;
                    hd.TrangThai = 1;
                    hd.ChucNang = ChucNang.NHAN_THONG_TIN;
                    MainForm.AddToQueueForm(hd);
                    MainForm.ShowQueueForm();
                }
            }       
        }
        private void showFukien()
        {
            Company.BLL.KDT.GC.HopDong hopdongKDT = Company.BLL.KDT.GC.HopDong.Load(HD.SoHopDong);
            Form[] forms = this.ParentForm.MdiChildren;
            for (int i = 0; i < forms.Length; i++)
            {
                if (forms[i].Name.ToString().Equals("ThemPK"+hopdongKDT.ID))
                {
                    forms[i].Activate();
                    return;
                }
            }
            Company.Interface.KDT.GC.PhuKienGCSendForm pk = new Company.Interface.KDT.GC.PhuKienGCSendForm();
            pk.phukien.HopDong_ID = hopdongKDT.ID;
            pk.Name = "ThemPK"+hopdongKDT.ID.ToString();
            pk.MdiParent = this.ParentForm; ;
            pk.Show();
        }

        private void dgPhuKien_LoadingRow(object sender, RowLoadEventArgs e)
        {
            string st = e.Row.Cells[4].Value.ToString();
            if (st == "-1")
                e.Row.Cells[4].Text = "Chưa khai báo";
            else if (st == "0")
                e.Row.Cells[4].Text = "Chưa duyệt";
            else if (st == "1")
                e.Row.Cells[4].Text = "Đã duyệt";
        }

        private void dgPhuKien_RowDoubleClick(object sender, RowActionEventArgs e)
        {
            Company.BLL.KDT.GC.HopDong hopdongKDT = Company.BLL.KDT.GC.HopDong.Load(HD.SoHopDong);                      
            Form[] forms = this.ParentForm.MdiChildren;
            for (int i = 0; i < forms.Length; i++)
            {
                if (forms[i].Name.ToString().Equals(hopdongKDT.ID))
                {
                    forms[i].Activate();
                    return;
                }
            }
            Company.Interface.KDT.GC.PhuKienGCSendForm pk = new Company.Interface.KDT.GC.PhuKienGCSendForm();
            pk.phukien.HopDong_ID = hopdongKDT.ID;
            pk.phukien.SoPhuKien = e.Row.Cells[1].Text;
            pk.phukien.NgayPhuKien = Convert.ToDateTime(e.Row.Cells[2].Text);
            pk.Name = hopdongKDT.ID.ToString();
            pk.MdiParent = this.ParentForm; ;
            pk.Show();
        }
   }
}
